import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/profile/widgets/widget_profile_rich.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/appbar/common_app_bar.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/input/common_text_field.dart';
import 'package:flutter_09_game/widgets/verify_input_box/src/verification_box.dart';
import 'package:flutter_09_game/widgets/widget_profile_item.dart';
import 'package:flutter_09_game/widgets/widget_verification_box.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'profile_edit_trc_logic.dart';

class ProfileEditTrcPage extends StatelessWidget {
  ProfileEditTrcPage({super.key});

  final ProfileEditTrcLogic logic = Get.put(ProfileEditTrcLogic());

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<ProfileEditTrcLogic>(
      appBar: CommonAppBar(
        title: LocaleKeys.modifyBetAddress.tr,
        actions: [
          GestureDetector(
            onTap: () {
              AppUserManager().showServiceSheet();
            },
            child: AutoImage(
              Assets.commonServiceBtn,
              width: 32.w,
            ),
          ),
          10.horizontalSpace,
        ],
      ),
      builder: (_) {
        return Container(
          padding: EdgeInsets.all(12.w),
          margin: EdgeInsets.all(16.w)
              .copyWith(bottom: 16 + ScreenUtil().bottomBarHeight),
          decoration: BoxDecoration(
              color: AppColor.navBgColor,
              borderRadius: BorderRadius.circular(16.w)),
          child: Column(
            children: [
              Expanded(
                  child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    WidgetProfileItem(
                        LocaleKeys.addressType.tr,
                        Container(
                          padding: EdgeInsets.symmetric(
                              vertical: 14.w, horizontal: 12.w),
                          decoration: BoxDecoration(
                              color: AppColor.color2D2F35,
                              borderRadius: BorderRadius.circular(8.w)),
                          child: Row(
                            children: [
                              Text(
                                LocaleKeys.trx20Protocol.tr,
                                style: TextStyle(
                                    color:
                                        AppColor.textPrimary.withOpacity(0.3),
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w500),
                              )
                            ],
                          ),
                        )),
                    16.verticalSpaceFromWidth,
                    WidgetProfileItem(LocaleKeys.fundPassword.tr,
                        WidgetVerificationBox(
                      onValueChanged: (text) {
                        logic.fundPassword.value = text;
                        logic.validateForm();
                      },
                    )),
                    16.verticalSpaceFromWidth,

                    Obx(() {
                      return WidgetProfileItem(
                          LocaleKeys.securitySafeQuestion.tr,
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 14.w, horizontal: 12.w),
                            decoration: BoxDecoration(
                                color: AppColor.color2D2F35,
                                borderRadius: BorderRadius.circular(8.w)),
                            child: Row(
                              children: [
                                Text(
                                  logic.securityQuestion.value.question
                                          ?.Content ??
                                      '',
                                  style: TextStyle(
                                      color:
                                          AppColor.textPrimary.withOpacity(0.3),
                                      fontSize: 14.sp,
                                      fontWeight: FontWeight.w500),
                                )
                              ],
                            ),
                          ));
                    }),
                    16.verticalSpaceFromWidth,

                    Obx(() {
                      return WidgetProfileItem(
                          (logic.securityQuestion.value.notice ?? '').isNotEmpty
                              ? '${LocaleKeys.securityAnswerHint.tr}:'
                              : LocaleKeys.enterSecurityAnswer.tr,
                          leftStr: logic.securityQuestion.value.notice,
                          CommonTextField(
                            textEditingController: logic.answerController,
                            hintText: LocaleKeys.enterSecurityAnswer.tr,
                            backgroundColor: Colors.black.withOpacity(0.3),
                            borderColor: AppColor.textPrimary.withOpacity(0.15),
                            maxLength: 100,
                            onChanged: (tex) {
                              logic.validateForm();
                            },
                          ));
                    }),
                    16.verticalSpaceFromWidth,
                    WidgetProfileItem(
                        LocaleKeys.currentAddress.tr,
                        Container(
                            width: double.infinity,
                            padding: EdgeInsets.symmetric(
                                vertical: 14.w, horizontal: 12.w),
                            decoration: BoxDecoration(
                                color: AppColor.color2D2F35,
                                borderRadius: BorderRadius.circular(8.w)),
                            child: Text(
                              maskString(AppUserManager()
                                      .user
                                      .value
                                      ?.withdrawAddress ??
                                  ""),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  color: AppColor.textPrimary.withOpacity(0.3),
                                  fontSize: 14.sp,
                                  fontWeight: FontWeight.w500),
                            ))),

                    16.verticalSpaceFromWidth,
                    WidgetProfileItem(
                        LocaleKeys.newBetAddress.tr,
                        CommonTextField(
                          textEditingController: logic.newAddressController,
                          hintText: LocaleKeys.pleaseEnterBettingAddress.tr,
                          backgroundColor: Colors.black.withOpacity(0.3),
                          borderColor: AppColor.textPrimary.withOpacity(0.15),
                          maxLength: 40,
                          inputFormatters: [
                            FilteringTextInputFormatter.allow(
                                RegExp(r'[a-zA-Z0-9]'))
                          ],
                          keyboardType: TextInputType.visiblePassword,
                          rightWidget: GestureDetector(
                            onTap: () {
                              UtilsOther.copyMemberId(
                                  logic.newAddressController.text);
                            },
                            child: AutoImage(
                              Assets.gameCopy,
                              width: 32.w,
                            ),
                          ),
                          onChanged: (tex) {
                            logic.validateForm();
                          },
                        )),
                    16.verticalSpaceFromWidth,
                    Text(
                      LocaleKeys.warmTips.tr,
                      style: TextStyle(
                          color: AppColor.primary,
                          fontWeight: FontWeight.w600,
                          fontSize: 12.sp),
                    ),
                    8.verticalSpaceFromWidth,
                    // 1、为了您的淡金安全，请保您添加的地址是去中心化钱包地址(Tron)。且与所选协议一致，否则您将会要失这笔资金:
                    // 2、绑定投注地址更换成功后，将在 24小时安全保护期后，方可发起提现操作，请合理安排淡金使用。
                    WidgetProfileRich(text: '1、${LocaleKeys.bindTrc20Tips.tr}'),
                    // 8.verticalSpaceFromWidth,

                    WidgetProfileRich(
                        text: '2、${LocaleKeys.addressSecurityTip2.tr}'),
                  ],
                ),
              )),
              12.verticalSpaceFromWidth,
              Obx(() => CommonButton.expend(
                    title: LocaleKeys.confirmEdit.tr,
                    enable: logic.isFormValid.value,
                    onClick: () {
                      logic.postUserChangeTrc20Address();
                    },
                  )),
            ],
          ),
        );
      },
    );
  }

  String maskString(String input) {
    if (input.length <= 8) return input; // 如果长度太短，不做处理

    String prefix = input.substring(0, 4);
    String suffix = input.substring(input.length - 4);
    return '$prefix***$suffix';
  }

// Widget _buildItem(String title, Widget child,
//     {String? leftStr, Widget? rightWidget}) {
//   return Column(
//     crossAxisAlignment: CrossAxisAlignment.start,
//     children: [
//       SizedBox(
//           height: 40.w,
//           child: Row(
//             crossAxisAlignment: CrossAxisAlignment.center,
//             children: [
//               RichText(
//                 text: TextSpan(
//                   text: title,
//                   style: TextStyle(
//                     color: AppColor.textPrimary.withOpacity(0.94),
//                     fontSize: 16.sp,
//                     fontWeight: FontWeight.w600,
//                   ),
//                   children: [
//                     if ((leftStr ?? '').isNotEmpty)
//                       TextSpan(
//                         text: leftStr ?? '',
//                         style: TextStyle(
//                           color: AppColor.textPrimary.withOpacity(0.94),
//                           fontSize: 16.sp,
//                           fontWeight: FontWeight.w600,
//                         ),
//                       ),
//                   ],
//                 ),
//               ),
//               const Spacer(),
//               if (rightWidget != null) rightWidget
//             ],
//           )),
//       4.verticalSpaceFromWidth,
//       child
//     ],
//   );
// }
}
